package com.zj.service;

import com.zj.dao.Myuser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class MyUserService implements IuserService {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Override
    @Transactional
    public int addUser(Myuser user) {
        String sql = "insert into myuser(user_name ,sex,birth_date,id_no,address) values(?,?,?,?,?)";
        int res = jdbcTemplate.update(sql, user.getUserName(), user.getSex(), user.getBirthDate(), user.getIdNo(), user.getAddress());
//        int a=1/0;
        return res;
    }

    @Override
    @Transactional
    public int deleteUser(Integer idno) {
        String sql = "delete from myuser where id_no =?";
        int res = jdbcTemplate.update(sql, idno);
        int a=1/0;
        return res;
    }

    @Override
    public int updateUser(Myuser user) {
        String sql = "update myuser set address=? where  id_no=?";
        int res = jdbcTemplate.update(sql, user.getAddress(), user.getIdNo());
//        int a=1/0;
        return res;
    }

    @Override
    @Transactional(readOnly = true)
    public Myuser selectUserById(Integer id) {
        String sql = "select * from myuser where id =?";
        Myuser res = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(Myuser.class), id);
        int a=1/0;
        return res;
    }

    @Override
    @Transactional(readOnly = true)
    public Myuser selectUser(Integer id) {
        String sql = "select * from myuser where id_no =?";
        Myuser res = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(Myuser.class), id);
//        int a=1/0;
        return res;
    }
}
